package club.ljwang.log;

import club.ljwang.pojo.SysLog;
import club.ljwang.service.LogService;
import club.ljwang.utils.IpAdrressUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * @description:
 * @author: ming
 * @date: 2020/3/9-14:56
 * @Version: 1.0.0
 */
@Component
@Aspect //切面
public class LogAspect {

    @Reference
    private LogService logService;

    @Pointcut("execution(* club.ljwang.controller.*.*(..))")//切入点
    public void operationLog() {
    }

    @Around(value = "operationLog()")
    public Object aroundService(ProceedingJoinPoint pjp) throws Throwable {
        //获取系统参数
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();


        Signature signature = pjp.getSignature();

        MethodSignature methodSignature = (MethodSignature)signature;

        //目标RequestMapping中的方法
        Method targetMethod = methodSignature.getMethod();
        RequestMapping annotation = targetMethod.getAnnotation(RequestMapping.class);

        SysLog sysLog = new SysLog();
        sysLog.setCreateDate(new Date());
        sysLog.setIp(IpAdrressUtil.getIpAdrress(request));
        sysLog.setMethod(JSON.toJSONString(annotation.value()));
        if(user != null){
            //获取当前登入的用户名
            sysLog.setUsername(user.getUsername());
        }

        Object obj;
        try {
            // 请求接收的参数args
            Object[] args = pjp.getArgs();

            String requestURI = request.getRequestURI();
            if (args!=null && args.length!=0 && !requestURI.equals("/report/exportBusinessReport1.do")) {

                sysLog.setParams(JSON.toJSONString(args));

            }
            //执行目标方法
            obj = pjp.proceed();

            sysLog.setResult(JSON.toJSONString(obj));

        } catch (Throwable t) {
            throw t;
        } finally {
            logService.recordLog(sysLog);
        }
        return obj;

    }
}
